package com.pozu.new_retail.controllers;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.dao.model.Ugc;
import com.pozu.new_retail.dao.vo.UgcVo;
import com.pozu.new_retail.service.UgcService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;
import com.pozu.new_retail.utils.Tools;

/**
 * UGC 帖子 | 用户自己发的帖子
 *
 * @author shenxueyuan
 * @Date 2019/3/5
 */
@Controller
public class UgcController {
	
	@Autowired
	private UgcService ugcService;
	
	/**
	 * 获取自己发布的帖子
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/api/ugcs/my", method = RequestMethod.GET)
	public Object getMyList(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		long userId = Long.parseLong(request.getHeader("userId"));
		
		String content = request.getParameter("content");//帖子内容
		
		int pageStart = Integer.parseInt(request.getParameter("pageStart"));//第几页从0开始
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));//一页显示几条数据		
		
		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("pageStart", pageStart*pageSize);
		map.put("pageSize", pageSize);
		map.put("userId", userId);
		map.put("content", content);
		map.put("appId", appId);

		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = ugcService.queryMyByMap(map);

		return jr;
	}
	
	/**
	 * 最新帖子列表 时间倒序排列
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/api/ugcs/list", method = RequestMethod.GET)
	public Object getList(HttpServletRequest request) throws Exception{

		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		long userId = Long.parseLong(request.getHeader("userId"));
		
		String content = request.getParameter("content");//帖子内容
		
		int pageStart = Integer.parseInt(request.getParameter("pageStart"));//第几页从0开始
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));//一页显示几条数据

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("pageStart", pageStart*pageSize);
		map.put("pageSize", pageSize);
		map.put("appId",appId);
		map.put("content", content);
		map.put("userId", userId);

		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = ugcService.queryByMap(map);

		return jr;
	}
	
	/**
	 * 精选 帖子
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/api/ugcs/listRec", method = RequestMethod.GET)
	public Object getListRec(HttpServletRequest request) throws Exception{

		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		long userId = Long.parseLong(request.getHeader("userId"));
		
		String content = request.getParameter("content");//帖子内容
		
		int pageStart = Integer.parseInt(request.getParameter("pageStart"));//第几页从0开始
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));//一页显示几条数据
		
		
		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("pageStart", pageStart*pageSize);
		map.put("pageSize", pageSize);
		map.put("appId", appId);
		map.put("content", content);
		map.put("userId", userId);

		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = ugcService.queryByRecMap(map);

		return jr;
	}
	
	/**
	 * 获取单个帖子信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/api/ugcs", method = RequestMethod.GET)
	public Object get(HttpServletRequest request) throws Exception{

		long ugcId = Integer.parseInt(request.getParameter("ugcId"));//帖子id
		
		long userId = Long.parseLong(request.getHeader("userId"));

		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = ugcService.queryByUgcId(ugcId,userId);

		return jr;
	}

	@ResponseBody
	@RequestMapping(value = "/api/ugcs", method = RequestMethod.POST)
	public Object add(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		long userId = Long.parseLong(request.getHeader("userId"));
		
		JsonResult jr = new JsonResult();
		
		Ugc requestUgc = Tools.getRequestObject(request, Ugc.class);
		requestUgc.setUserId(userId);
		requestUgc.setAppId(appId);
		
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);		
		
		ugcService.saveUgc(requestUgc);
		
		return jr;
	}
	
	/**
	 * 删除帖子
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/api/ugcs", method = RequestMethod.PUT)
	public Object put(HttpServletRequest request) throws Exception{
		
		long userId = Long.parseLong(request.getHeader("userId"));
		
		Ugc requestUgc = Tools.getRequestObject(request, Ugc.class);
		
		JsonResult jr = new JsonResult();

		UgcVo ugcVo = ugcService.queryByUgcId(requestUgc.getUgcId(),userId);
		if(null == ugcVo){
			jr.respCode = CodeConstants.UGCNOTEXISTED_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.UGCNOTEXISTED_MSG);
			return jr;
		}

		Ugc temp = new Ugc();
		
		temp.setUgcId(requestUgc.getUgcId());
		temp.setState(1);
		
		ugcService.updateUgc(temp);
		
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);		

		return jr;
	}
	
}
